#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll MOD = 998244353;

void solve() {
	ll n;
	cin >> n;
	vector<pair<int, int>> a(n + 1);
	int ans = 0;
	for (int i = 1, x, y; i <= n; i++) {
		cin >> x >> y;
		if (y >= 3) {
			ans++;
		}
		a[i] = {x, y};
	}
	sort(a.begin() + 1, a.end());
	for (int i = 1; i <= n; i++) {
		auto [x, c] = a[i];
		if (c >= 2) {
			pair<int, int> t = {x * 2, -1};
			int cnt = lower_bound(a.begin() + 1, a.end(), t) - a.begin()- 1;
			if (cnt > 0) {
				ans += cnt - 1;
			}
		}

	}
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			pair t = {a[i].first + a[j].first, -1};
			int cnt = lower_bound(a.begin() + j + 1, a.end(), t) - a.begin() - j - 1;
			ans += cnt;
		}
	}
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t; cin >> t; while (t-- > 0) solve();
	return 0;
}